package com.nodecollege.cloud.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.nodecollege.cloud.common.constants.NCConstants;
import com.nodecollege.cloud.common.exception.UpmsException;
import com.nodecollege.cloud.common.model.NCResult;
import com.nodecollege.cloud.common.model.QueryVO;
import com.nodecollege.cloud.common.model.po.UpmsApp;
import com.nodecollege.cloud.common.model.po.UpmsRole;
import com.nodecollege.cloud.common.model.po.UpmsRoleMenu;
import com.nodecollege.cloud.common.model.po.UpmsTenant;
import com.nodecollege.cloud.common.utils.NCUtils;
import com.nodecollege.cloud.dao.mapper.UpmsAppMapper;
import com.nodecollege.cloud.dao.mapper.UpmsRoleMapper;
import com.nodecollege.cloud.dao.mapper.UpmsRoleMenuMapper;
import com.nodecollege.cloud.dao.mapper.UpmsTenantMapper;
import com.nodecollege.cloud.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author LC
 * @date 2019/12/17 16:43
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private UpmsRoleMapper upmsRoleMapper;

    @Autowired
    private UpmsTenantMapper upmsTenantMapper;

    @Autowired
    private UpmsAppMapper upmsAppMapper;

    @Autowired
    private UpmsRoleMenuMapper upmsRoleMenuMapper;

    /**
     * 运维管理 应用管理 查询预制角色 appId 必填
     * 系统管理 角色管理 tenantId必填
     *
     * @param queryVO
     * @return
     */
    @Override
    public NCResult<UpmsRole> getRoleList(QueryVO<UpmsRole> queryVO) {
        NCUtils.nullOrEmptyThrow(queryVO.getData().getRoleSource(), new UpmsException("-1", "角色来源不能为空！"));
        List<UpmsRole> list = new ArrayList<>();
        Long total = 0L;
        if (NCConstants.INT_0.equals(queryVO.getData().getRoleSource())) {
            if (NCUtils.isNullOrEmpty(queryVO.getData().getAppId()) && NCUtils.isNullOrEmpty(queryVO.getLongList())) {
                throw new UpmsException("-1", "应用id 或者应用idList不能为空！");
            }
        } else {
            NCUtils.nullOrEmptyThrow(queryVO.getTenantId(), new UpmsException("-1", "租户id不能为空！"));
            queryVO.getData().setRoleSource(null);
        }
        if (NCConstants.INT_NEGATIVE_1.equals(queryVO.getPageSize())) {
            list = upmsRoleMapper.selectListByMap(queryVO.toMap());
            total = NCUtils.isNullOrEmpty(list) ? 0 : Long.parseLong(list.size() + "");
        } else {
            Page page = PageHelper.startPage(queryVO.getPageNum(), queryVO.getPageSize());
            if (queryVO.isSort()) {
                page.setOrderBy(queryVO.getSortKey() + " " + queryVO.getSortDirection());
            }
            list = upmsRoleMapper.selectListByMap(queryVO.toMap());
            total = page.getTotal();
        }
        return NCResult.ok(list, total);
    }

    /**
     * 添加角色
     *
     * @param upmsRole
     */
    @Override
    public void addRole(UpmsRole upmsRole) {
        NCUtils.nullOrEmptyThrow(upmsRole.getRoleName(), new UpmsException("-1", "角色名称必填！"));
        NCUtils.nullOrEmptyThrow(upmsRole.getRoleCode(), new UpmsException("-1", "角色代码必填！"));
        NCUtils.nullOrEmptyThrow(upmsRole.getRoleSource(), new UpmsException("-1", "角色来源必填！"));
        NCUtils.nullOrEmptyThrow(upmsRole.getRoleType(), new UpmsException("-1", "角色类型必填！"));
        NCUtils.nullOrEmptyThrow(upmsRole.getDataPower(), new UpmsException("-1", "角色数据权限必填！"));
        UpmsRole queryRole = new UpmsRole();
        if (NCConstants.INT_0.equals(upmsRole.getRoleSource())) {
            NCUtils.nullOrEmptyThrow(upmsRole.getAppId(), new UpmsException("-1", "应用id不能为空！"));
            UpmsApp exApp = upmsAppMapper.selectByPrimaryKey(upmsRole.getAppId());
            NCUtils.nullOrEmptyThrow(exApp, new UpmsException("", "该应用不存在！"));
        } else {
            NCUtils.nullOrEmptyThrow(upmsRole.getTenantId(), new UpmsException("-1", "租户id不能为空！"));
            queryRole.setTenantId(upmsRole.getTenantId());
            UpmsTenant exTenant = upmsTenantMapper.selectByPrimaryKey(upmsRole.getTenantId());
            NCUtils.nullOrEmptyThrow(exTenant, new UpmsException("", "该租户不存在！"));
        }
        queryRole.setRoleCode(upmsRole.getRoleCode());
        List<UpmsRole> exList = upmsRoleMapper.selectListByMap(new QueryVO<>(queryRole).toMap());
        NCUtils.notNullOrNotEmptyThrow(exList, new UpmsException("-1", "角色代码已存在！"));

        upmsRole.setState(NCConstants.INT_1);
        upmsRole.setCreateTime(new Date());
        upmsRoleMapper.insertSelective(upmsRole);
    }

    /**
     * 更新角色
     *
     * @param upmsRole
     */
    @Override
    public void updateRole(UpmsRole upmsRole) {
        NCUtils.nullOrEmptyThrow(upmsRole.getRoleId(), new UpmsException("-1", "角色id必填！"));
        NCUtils.nullOrEmptyThrow(upmsRole.getRoleSource(), new UpmsException("-1", "角色来源必填！"));
        UpmsRole exRole = upmsRoleMapper.selectByPrimaryKey(upmsRole.getRoleId());
        NCUtils.nullOrEmptyThrow(exRole, new UpmsException("", "角色不存在！"));
        if (!exRole.getRoleSource().equals(upmsRole.getRoleSource())) {
            throw new UpmsException("", "不能操作该来源角色！");
        }
        if (NCConstants.INT_1.equals(exRole.getRoleSource())) {
            NCUtils.nullOrEmptyThrow(upmsRole.getTenantId(), new UpmsException("", "租户id必传！"));
            if (!exRole.getTenantId().equals(upmsRole.getTenantId())) {
                throw new UpmsException("", "角色不存在！");
            }
        }
        if (NCUtils.isNotNullOrNotEmpty(upmsRole.getRoleCode())) {
            UpmsRole queryRole = new UpmsRole();
            if (NCConstants.INT_1.equals(exRole.getRoleSource())) {
                queryRole.setTenantId(exRole.getTenantId());
            }
            queryRole.setRoleCode(upmsRole.getRoleCode());
            List<UpmsRole> exList = upmsRoleMapper.selectListByMap(new QueryVO<>(queryRole).toMap());
            if (exList.size() == 1 && !exList.get(0).getRoleId().equals(exRole.getRoleId())) {
                throw new UpmsException("-1", "角色代码已存在！");
            }
        }

        UpmsRole updateRole = new UpmsRole();
        updateRole.setRoleId(upmsRole.getRoleId());
        updateRole.setRoleName(upmsRole.getRoleName());
        updateRole.setRoleCode(upmsRole.getRoleCode());
        updateRole.setDataPower(upmsRole.getDataPower());
        updateRole.setNum(upmsRole.getNum());
        upmsRoleMapper.updateByPrimaryKeySelective(updateRole);
    }

    /**
     * 删除角色
     *
     * @param upmsRole
     */
    @Override
    public void delRole(UpmsRole upmsRole) {
        NCUtils.nullOrEmptyThrow(upmsRole.getRoleId(), new UpmsException("-1", "角色id必填！"));
        NCUtils.nullOrEmptyThrow(upmsRole.getRoleSource(), new UpmsException("-1", "角色来源必填！"));
        UpmsRole exRole = upmsRoleMapper.selectByPrimaryKey(upmsRole.getRoleId());
        NCUtils.nullOrEmptyThrow(exRole, new UpmsException("", "角色不存在！"));
        if (!exRole.getRoleSource().equals(upmsRole.getRoleSource())) {
            throw new UpmsException("", "不能操作该来源角色！");
        }
        UpmsRoleMenu queryRoleMenu = new UpmsRoleMenu();
        queryRoleMenu.setRoleId(upmsRole.getRoleId());
        if (NCConstants.INT_1.equals(exRole.getRoleSource())) {
            NCUtils.nullOrEmptyThrow(upmsRole.getTenantId(), new UpmsException("", "租户id必传！"));
            if (!exRole.getTenantId().equals(upmsRole.getTenantId())) {
                throw new UpmsException("", "角色不存在！");
            }
            queryRoleMenu.setTenantId(upmsRole.getTenantId());
        }

        List<UpmsRoleMenu> exRoleMenuList = upmsRoleMenuMapper.selectListByRoleMenu(queryRoleMenu);
        NCUtils.notNullOrNotEmptyThrow(exRoleMenuList, new UpmsException("", "该角色存在绑定菜单，请先移除绑定的菜单！"));

        if (NCConstants.INT_0.equals(exRole.getState())) {
            throw new UpmsException("-1", "该角色不能删除！");
        }

        UpmsRole delRole = new UpmsRole();
        delRole.setState(NCConstants.INT_NEGATIVE_1);
        delRole.setRoleId(upmsRole.getRoleId());
        upmsRoleMapper.updateByPrimaryKeySelective(delRole);
    }

    /**
     * 锁定解锁角色
     *
     * @param upmsRole
     */
    @Override
    public void lockRole(UpmsRole upmsRole) {
        NCUtils.nullOrEmptyThrow(upmsRole.getRoleId(), new UpmsException("-1", "角色id必填！"));
        NCUtils.nullOrEmptyThrow(upmsRole.getRoleSource(), new UpmsException("-1", "角色来源必填！"));
        UpmsRole exRole = upmsRoleMapper.selectByPrimaryKey(upmsRole.getRoleId());
        NCUtils.nullOrEmptyThrow(exRole, new UpmsException("", "角色不存在！"));
        if (!exRole.getRoleSource().equals(upmsRole.getRoleSource())) {
            throw new UpmsException("", "不能操作该来源角色！");
        }
        if (NCConstants.INT_1.equals(exRole.getRoleSource())) {
            NCUtils.nullOrEmptyThrow(upmsRole.getTenantId(), new UpmsException("", "租户id必传！"));
            if (!exRole.getTenantId().equals(upmsRole.getTenantId())) {
                throw new UpmsException("", "角色不存在！");
            }
        }

        if (NCConstants.INT_0.equals(exRole.getState())) {
            throw new UpmsException("-1", "该角色不能冻结！");
        }
        UpmsRole lockRole = new UpmsRole();
        lockRole.setRoleId(upmsRole.getRoleId());
        lockRole.setState(upmsRole.getState());
        upmsRoleMapper.updateByPrimaryKeySelective(lockRole);
    }
}
